#!/usr/bin/env python
#-*-coding:utf-8-*-

"""
@name : 函数定义
@author : wenyao
@projectname: sanchuanglianxi
"""

#函数的定义
# def  add(x, y):
#     return  x + y
#
# a = add(1,2)
# print(a)

# import sys
# print("hello","world")
# #(sys.stdout)

# def print_test():
#     print("test def")
#     return None
#
# a = print_test()
# print(a)

# def bmi():
#     s = float(input("请输入身高："))
#     h = float(input("请输入体重:"))
#     bmi = h/s**2
#     print(bmi)
#
# bmi()
#    return  bmi
#
# def bmi( s , h):
#     # s = float(input("请输入身高："))
#     # h = float(input("请输入体重:"))
#     # bmi = h/s**2
#     # print(bmi)
#     print(s ,h)
#     print(id(s), id(h))
#     s = 18
#     h = 80
#     print(s , h)
#     print(id(s),id(h))
#
# s=19
# h=43
# print(id(s),id(h))
# bmi(s,h)
# print(s,h)
# bmi(1.8, 70)

#默认参数 位置参数 关键字参数
#注意使用顺序
# def userinfo(name, age: int, sex = "f"):
#     print(type(age))
#     print(f"username is {name}, userage is {age}, usersex is {sex}")

# userinfo("wen", sex = "m", age = 18)
# userinfo("wen",'haha','f')
# userinfo(18,"wen","f")
# # userinfo(name="wen", age=18, sex='f')
# userinfo(age='18', name = "wen", sex = "f")
# userinfo(age = "18", name = "wen", sex = "f")
# userinfo("wen","18")
# userinfo("wen", sex = "f", age = "18")
# userinfo(name = "wen", 18, 'f')

#默认参数
# def add(x, y, z, h, g = "haha"):
#     result =  x + y + z +h
#     print(g)
#     print(result)
#
# add(1,2,3,4,"xx")

#print("hello", "world", sep = "####")

#可变长参数传递
#tuple 元组
# def myargs(*num):
#     print(type(num), num)
#
# myargs(1)
# myargs(1,2)
# myargs(1,2,3)
# myargs(1,2,3,4)
#
# def mykwargs(**testkwargs):
#     print(type(testkwargs), testkwargs)
#
# mykwargs(a="1", b="2", c="3")
# mykwargs(name="1", age="2")

#设定期望参数类型，以及期望返回类型，传递的数值不一定得是指定类型，只是指定之后在pycharm中有提示。
# def teststr(a: str)  ->str:   #期望是个str，不一定非得是str
#     print(a,type(a))
#     return 1                   #期望返回str
#
# b = teststr(12)
# print(b,type(b))

#a = [1,2,3]  -》列表
#b = (1,2,3)  -->元组
#c = {"a":1,"b":2}  #字典  ==》  a=1,b=2
#print(c['a'])

#可变长位置参数
# packing  and unpacking
# 1,2,3,4  ---> packing --> num -->元组
#*num --> unpacking  --> 1 2 3 4
def myargs1(num):
    sum = 0
    a = "haha"
    print(num)
    #print(*num)
    # for i in num:
    #     sum += i
    #     print(sum,i)
    # return sum, a
# myargs1(*[1,2,3,4])
#myargs1(1 2 3 4)
# print(a, b)
# myargs1(1,2,3)
# num_lst = [1,2,3,4]
# print(*num_lst)
# myargs1(*num_lst)

# list = [1,2,3,4]
# print(*list)
# print("str is {} {} {} {}".format(*list))

#可变长关键字位置参数:
#packing  a=1,b=2 -->packing -->{"a":1,"b":2}-->num 字典
#unpacking **num  --> a = 1 , b=2

# def mykwargs(**num):
#     sum = 0
#     print(num)
#     print(num.values())
#     for i in num.values():
#         sum += i
#     print(sum)
# mykwargs(a=1, b = 2, c = 3, d = 4)
# a = {"name":"wen", "age":18, "sex":"f"}
#**a ==>  name="wen",age=18,"sex"="f"
# print("name is {name}, age is {age}, sex is {sex}".format(**a))
#"name is {name}, age is {age}, sex is {sex}".format(name="wen",age=18,sex="f")

#匿名函数 --没有名字的函数
# def add(x,y):
#     return x + y
# a = add(1,2)
# print(a)

# testlambda = lambda  x ,y : x +y
# print("lambada result is ",testlambda(1,2) )
#
# testlst = lambda x:x[0]
# print(testlst([1,2,3]))
# print(testlst(["haha","hehe","xixi"]))

#匿名函数设置默认参数
# testlambda = lambda x, y =2: x +y
# print(testlambda(1))

#匿名函数设置可变长参数
# testlambda = lambda *args: args
# print(testlambda(1,2,3,4))

# a = [-10,2,0,-1,9,-7]
# #sorted
# print(sorted(a, key= lambda x: x if x>0 else -x))

#阶乘
#n! = 1*2*3*4....*n
#f(6) == 6 * f(5)
#f(n) = n * f(n-1)
#递归函数
# def jiecheng(n):
#     #定义出口
#     if n == 1:
#         return 1
#     else:
#         return n*jiecheng(n-1)
# print(jiecheng(50))

#函数参数传递，实际是传递引用
# def f(x):
#     print(x)
#
#     x = 100
#     print(x)
# a = 1
# f(a)
# print(a)

# def f(x):
#     x[0] = 100
#     print(x)
#
# a = [1,2,3]
# f(a)
# print(a)

#默认参数最好不要设置成可变类型
# def defultlst(a=[]):
#     a.append(1)
#     print(a)
#
# defultlst()
# defultlst()

# gloabl 在函数内声明使用全局变量
# def func():
#     global x
#     x = 2
#     a = 3
#     print('x is ',x)
#
# x = 60
# func()
# print("x is stil,", x)


#命名空间，变量的作用域
#a=1  {"a":1}

# def namespacetest():
#     #__name__ = "haha"
#     # print(a)
#     a = 3
#     def namespacetest1():
#        # __name__ = "haha"
#        #a = 2
#        b = 3
#        print(a)
#        print(locals())
#        print(globals())
#     namespacetest1()
# c = 100
# #__name__ = "haha"
# namespacetest()

#作页
# >>> a="str"
# >>> isinstance(a,str)
# True

#工厂函数
# a = 1
# b = str(a)
# b.isupper()

#help(__builtins__)
# 用户密码复杂度验证(封装成函数)
# '''长度校验'''
# def changdu(password):
#     if len(password) >= 8:
#         return 1
#     else:
#         return 0
#     a = 1 if len(password)>= 8 else 0
#     return a
#
#
# '''复杂度校验'''  # 下面为两种方法都可以
# a = [0,0,0,0]
# def fuzadu(password):
#     for i in password:
#         if i.isupper():
#             a[0] = 1
#         elif i.islower():
#             a[1] = 1
#         elif i.isdigit():
#             a[2] = 1
#         else:
#             a[3] = 1
#     if sum(a) >= 3:
#         return 1
#     else:
#         return 0
#
# # def fuzadu(password):
# #     shuzi = 0
# #     zimud = 0
# #     zimux = 0
# #     qita = 0
# #     for i in password:
# #         if (ord(i) >= 48 and ord(i) <= 57):   #判断是否为数字
# #             shuzi = 1
# #         elif (ord(i) >= 65 and ord(i) <= 90):  #判断是否为大写字母
# #             zimud = 1
# #         elif (ord(i) >= 97 and ord(i) <= 122):  # 判断是否为小写字母
# #             zimux = 1
# #         elif ord(i) >= 33 and ord(i) <= 47 or ord(i) >= 58 and ord(i) <= 64 or ord(i) >= 91 or ord(i) <= 96: #判断是否为其他字符
# #             qita = 1
# #
# #     if shuzi+zimud+zimux+qita >= 3:
# #         return 1
# #     else:
# #         return 0
#
# '''重复度校验'''
# #abcdefghij
# #abc
# def chongfudu(password):
#     pass_len = len(password)
#     for i in range(pass_len - 4):
#         str1 = password[i:i + 3]
#         #count = password.count(str1)
#         str2 = password[i + 3:]
#         if str1 in str2:
#             return 0
#     return 1
#
# '''分数系统'''
# def grade(password):
#     grade = changdu(password) + fuzadu(password) + chongfudu(password) +2
#     if grade >=4:
#         if chongfudu(password) == 0:
#             print(f'注册成功，密码强度为{grade},但是密码重复度过高')
#         else:
#             print(f'注册成功，密码强度为{grade}')
#     else:
#         print('注册失败')
#
# '''密码校验'''
# def  password_check():
#     password1 = input('请输入密码：').replace(' ','')# 去除密码中的空格
#     password2 = input('请再次输入密码：').replace(' ','')
#     if password1 == password2:
#         #changdu(password1)
#         if changdu(password1) == 0:
#             print('密码长度不够，请增加密码长度')
#         #fuzadu(password1)
#         if fuzadu(password1) == 0:
#             print('密码复杂度不够,请增加密码复杂度')
#         #chongfudu(password1)
#         grade(password1)
#
#     else:
#         print('两次密码输入不一致')
#
# password_check()